package cn.matezk.lianlian.task;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SmsSendTask {
    @Autowired
    private Client client;

    @XxlJob("smsSendTask")
    public void execute() {
        JSONArray array = JSONUtil.parseArray(XxlJobHelper.getJobParam());
        for (int i = 0; i < array.size(); i ++) {
            JSONObject item = array.getJSONObject(i);
            String sign = item.getStr("sign");
            String mobile = item.getStr("mobile");
            String templateCode = item.getStr("templateCode");
            String params = item.getStr("params");
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(mobile);
            request.setSignName(sign);
            request.setTemplateCode(templateCode);
            request.setTemplateParam(params);
            request.setOutId("lianlian");
            try {
                SendSmsResponse response = client.sendSms(request);
                XxlJobHelper.log(JSONUtil.toJsonStr(response));
            } catch (Exception e) {
                log.error(e.getMessage());
                XxlJobHelper.log(e);
            }
        }
    }
}
